﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Configuration;

namespace ReportsFactory
{
    public static class Common
    {

        public static string Username
        {
            get
            {
                if (HttpContext.Current != null)
                    return HttpContext.Current.User.Identity.Name.ToString();
                else
                    return String.Empty;
            }
        }

        public static string GetNewReportFileName()
        {
            string fileName = ReportsFolder + Guid.NewGuid().ToString() + ".xml";
            while (File.Exists(HttpContext.Current.Server.MapPath(fileName)))
                fileName = ReportsFolder + Guid.NewGuid().ToString() + ".xml";

            return fileName;
        }

        public static List<string> GetAllReportFileNames()
        {
            DirectoryInfo di = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/Reports"));
            FileInfo[] xmlFiles = di.GetFiles("*.xml");

            List<string> rtnVal = new List<string>();

            foreach (FileInfo fi in xmlFiles)
                rtnVal.Add(fi.Name);

            return rtnVal;
        }

        public static string ReportsFolder
        {
            get
            {
                return ConfigurationManager.AppSettings[Config.REPORTS_FOLDER];
            }
        }
    }
}